package com.xlh.service.courseTest.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.xlh.dao.courseTest.ExperimentStatisticsStepPassMapper;
import com.xlh.param.courseTest.ExperimentRunJudgeParam;
import com.xlh.pojo.courseTest.ExperimentStatisticsStepPass;
import com.xlh.pojo.courseTest.example.ExperimentStatisticsStepPassExample;
import com.xlh.security.UserInfoHolder;
import com.xlh.service.courseTest.ExperimentStatisticsStepPassService;
import com.xlh.util.BeanUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author cfz
 */
@Service
@Slf4j
public class ExperimentStatisticsStepPassServiceImpl implements ExperimentStatisticsStepPassService {

    @Autowired
    private ExperimentStatisticsStepPassMapper experimentStatisticsStepPassMapper;

    @Autowired
    UserInfoHolder userInfoHolder;

    @Override
    public int save(ExperimentStatisticsStepPass record) {

        Integer changeRecord = 0;
        ExperimentStatisticsStepPassExample example = new ExperimentStatisticsStepPassExample();
        example.createCriteria().andIdEqualTo(record.getId()).andDeletedEqualTo(false);
        List<ExperimentStatisticsStepPass> stepPasses = experimentStatisticsStepPassMapper.selectByExample(example);


        return changeRecord;
    }

    @Override
    public List<ExperimentStatisticsStepPass> selectByExample(ExperimentStatisticsStepPassExample example) {

        return experimentStatisticsStepPassMapper.selectByExample(example);
    }

    @Override
    public void saveStepPass(ExperimentRunJudgeParam experimentRunJudgeParam) {

        // 装载对象
        ExperimentStatisticsStepPass stepPass = new ExperimentStatisticsStepPass();
        stepPass.setType(experimentRunJudgeParam.getType().intValue()).setContextId(experimentRunJudgeParam.getContextId().intValue());
        stepPass.setClassId(experimentRunJudgeParam.getClassId().intValue()).setUserId(userInfoHolder.getUserId().intValue());
        stepPass.setCourseId(experimentRunJudgeParam.getCourseId().intValue()).setPass((byte) 1);

        // 查询是否已经存在
        ExperimentStatisticsStepPassExample stepPassExample = new ExperimentStatisticsStepPassExample();
        stepPassExample.createCriteria()
                .andTypeEqualTo(stepPass.getType()).andDeletedEqualTo(false).andContextIdEqualTo(stepPass.getContextId())
                .andCourseIdEqualTo(stepPass.getCourseId()).andUserIdEqualTo(stepPass.getUserId()).andClassIdEqualTo(stepPass.getClassId());
        List<ExperimentStatisticsStepPass> stepPasses = experimentStatisticsStepPassMapper.selectByExample(stepPassExample);
        if(CollectionUtil.isEmpty(stepPasses)){
            experimentStatisticsStepPassMapper.insert(stepPass);
        }
    }


}
